package com.longtop.framework.sys.org.web;

import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.org.model.TSysOrg;
import com.longtop.framework.sys.org.service.OrgService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 系统管理-机构
 * 
 * @author SJH
 * 
 */
public class OrgListAction extends BaseAction {

	private final String DATASET_ORG = "Dataset_Org";

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		commonQuery(context, true);
	}

	/**
	 * 加载
	 * 
	 * @param context
	 */
	public void loadOrg(IActionContext context) {
		commonQuery(context, false);
	}

	/**
	 * private:读取机构
	 * 
	 * @param context
	 * @param isRoot
	 *            是否延迟加载
	 */
	private void commonQuery(Object context, boolean isRoot) {

		Dataset dataset = this.getDataset(context, DATASET_ORG);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_ORG)) {
			return;
		}

		QueryConditions conditions = WebUtil.createQueryConditions();
		if (isRoot) {
			conditions.setConditionIsNull("parentOrgId");
		}

		String filter = dataset.getFilter();

		OrgService orgService = this.getBean(OrgService.BEAN_ID);

		QueryResult queryResult = orgService.loadOrg(conditions, filter);

		try {
			BeanUtil.queryResultToDataset(queryResult, dataset, false, true);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("加载机构树出错。");
		}
	}

	/**
	 * 提交保存（增，删，改）
	 * 
	 * @param context
	 */
	public void saveOrg(IActionContext context) {
		Dataset orgDataset = (Dataset) context.getDataset(DATASET_ORG);
		if (!WebUtil.validateDatasetChange(this, orgDataset, DATASET_ORG)) {// DatasetChange
			return;
		}

		try {
			OrgService orgService = this.getBean(OrgService.BEAN_ID);

			Map changeMap = WebUtil.datasetToObjectList(orgDataset, "orgId", TSysOrg.class);

			Integer[] count = orgService.saveOrg(changeMap);

			String msg = WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue());

			this.hintInfoMessage(msg);
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());// 自定义的错误
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存机构失败。");
		}
	}

	/**
	 * 删除机构
	 * 
	 * @param context
	 *            web上下文
	 * @throws Exception
	 */
	public void deleteOrg(IActionContext context) throws Exception {
		String id = context.getCommandParameterValue("id");
		try {
			OrgService orgService = this.getBean(OrgService.BEAN_ID);

			SessionObject sessionObject = this.getSessionObject(context);
			orgService.deleteOrg(id, sessionObject);
		} catch (Exception e) {
			this.hintErrorMessage(e.getMessage());
		}
	}

	/**
	 * 判断是否有下级机构，是否可以删除的判断条件。
	 * 
	 * @param context
	 *            WEB上下文
	 */
	public void hasChild(IActionContext context) {
		String id = context.getCommandParameterValue("id");

		// 设置查询条件列表
		QueryConditions queryConditions = WebUtil.createQueryConditions();
		queryConditions.setCondition("parentOrgId", QueryConditions.EQUAL, id);

		OrgService orgService = this.getBean(OrgService.BEAN_ID);

		QueryResult queryResult = orgService.loadOrg(queryConditions, null);
		if (queryResult.getResultList().size() > 0) {
			context.addCommandParameter("hasChild", "true");
		} else {
			context.addCommandParameter("hasChild", "false");
		}
	}
}